#include "stdafx.h"

#include "PFData.h"

#include "PFItemNode.h"

PFItemNode::PFItemNode(PFData * model, PFItemNode * parent /*= 0*/)
	: parent(parent)
	, m_pModel(model)
{

}

PFItemNode::PFItemNode(const QString & text, PFData * model, PFItemNode * parent /*= 0*/)
	: parent(parent)
	, m_pModel(model)
	, m_strName(text)
{

}

PFItemNode::~PFItemNode()
{
	auto iter = children.begin();
	while (iter != children.end()) 
	{
		PFItemNode * node = *iter;
		iter = children.erase(iter);
		node->parent = nullptr;

		delete node;
		node = nullptr;
	}

	if (parent)
	{
		parent->RemoveChild(this);
		parent = 0;
	}
}

PFItemNode * PFItemNode::IndexNode(const QModelIndex & index)
{
	PFItemNode * node = static_cast<PFItemNode *>(index.internalPointer());
	return node;
}

void PFItemNode::RemoveChild(PFItemNode * child)
{
	auto iter = std::find(children.begin(), children.end(), child);
	if (iter != children.end())
	{
		child->parent = nullptr;
		children.erase(iter);
	}
}

void PFItemNode::AppendChild(PFItemNode * child)
{
	child->parent = this;
	child->m_iLevel = m_iLevel + 1;
	children.append(child);
}

